package site.xuxingcan;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import site.xuxingcan.Model.HongBaoModel;
import site.xuxingcan.Model.UserModel;
import site.xuxingcan.Model.YazhuModel;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
//        try {
//            FileHelper.getInstance().writeUserData("111fuck");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(8191);
                    while (true) {
                        final Socket socket = serverSocket.accept();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    SocketHelper socketHelper = new SocketHelper(socket);
                                    Gson gson = new Gson();
                                    while (true) {
                                        String s = socketHelper.readLine();
                                        if (s == null) {
                                            break;
                                        }
                                        if (s.equalsIgnoreCase(ActionUtil.GET_ALL_USER)) {
                                            socketHelper.writeLineAndFlush(gson.toJson(FileHelper.getInstance().getUserModelList()));
                                            continue;
                                        }
                                        if (s.equalsIgnoreCase(ActionUtil.UPDATE_USER)) {
                                            String json = socketHelper.readLine();
                                            UserModel model = gson.fromJson(json, UserModel.class);
                                            FileHelper.getInstance().updateUser(model);
                                            socketHelper.writeLineAndFlush(gson.toJson(FileHelper.getInstance().getUserModelList()));
                                            continue;
                                        }
                                        if (s.equalsIgnoreCase(ActionUtil.COMPUTE_YAZHU_USER)) {
                                            String json = socketHelper.readLine();
                                            ArrayList<YazhuModel> list = gson.fromJson(json,
                                                    new TypeToken<ArrayList<YazhuModel>>() {
                                                    }.getType());
                                            socketHelper.writeLineAndFlush(gson.toJson(FileHelper.getInstance().computeYazhuUsers(list)));
                                            continue;
                                        }
                                        if (s.equalsIgnoreCase(ActionUtil.COMPUTE_HONGBAO)) {
                                            String json = socketHelper.readLine();
                                            ArrayList<HongBaoModel> list = gson.fromJson(json,
                                                    new TypeToken<ArrayList<HongBaoModel>>() {
                                                    }.getType());
                                            socketHelper.writeLineAndFlush(gson.toJson(FileHelper.getInstance().computeHongBaoResult(list)));
                                            continue;
                                        }
                                        socketHelper.writeLineAndFlush("nothing");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
